var app = getApp();

Page({
    data: {
        System:'',
        djss: !1,
        luntext: [ "附近", "新入", "热门" ],
        searchAct: false,
        user_info_show:false,
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 35,
        currentTab: 0,
        swiperCurrent: 0,
        indicatorDots: !1,
        autoplay: !0,
        interval: 5e3,
        duration: 1e3,
        circular: !0,
        refresh_top: !1,
        star: [ {
            img: "../image/xing.png"
        }, {
            img: "../image/xing.png"
        }, {
            img: "../image/xing.png"
        }, {
            img: "../image/xing.png"
        }, {
            img: "../image/xing.png"
        } ],
        star1: [ 
          {
              img: "../image/xing.png"
          }, {
              img: "../image/star_none.png"
          }, {
              img: "../image/star_none.png"
          }, {
              img: "../image/star_none.png"
          }, {
              img: "../image/star_none.png"
          } 
        ],
        star2: [ 
          {
            img: "../image/xing.png"
          }, {
              img: "../image/xing.png"
          }, {
              img: "../image/star_none.png"
          }, {
              img: "../image/star_none.png"
          }, {
              img: "../image/star_none.png"
          } 
        ],
        star3: [ 
          {
              img: "../image/xing.png"
          }, {
              img: "../image/xing.png"
          }, {
              img: "../image/xing.png"
          }, {
              img: "../image/star_none.png"
          }, {
              img: "../image/star_none.png"
          } 
        ],
        star4: [ 
          {
            img: "../image/xing.png"
          }, {
              img: "../image/xing.png"
          }, {
              img: "../image/xing.png"
          }, {
              img: "../image/xing.png"
          }, {
              img: "../image/star_none.png"
          } 
        ]
    },
    searchTap(){
      this.setData({
        searchAct: !this.data.searchAct
      });
    },
    hddb: function() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        });
    },
    swiperChange: function(t) {
        this.setData({
            swiperCurrent: t.detail.current
        });
    },
    changeIndicatorDots: function(t) {
        this.setData({
            indicatorDots: !this.data.indicatorDots
        });
    },
    changeAutoplay: function(t) {
        this.setData({
            autoplay: !this.data.autoplay
        });
    },
    intervalChange: function(t) {
        this.setData({
            interval: t.detail.value
        });
    },
    durationChange: function(t) {
        this.setData({
            duration: t.detail.value
        });
    },
    tabClick: function(t) {
        var a = t.currentTarget.id;
        console.log(this.data);
        var e = this.data.business;
        if (null != this.data.business && 0 != e.length) {
            if (0 == a) this.refresh(); else if (1 == a) console.log(this.data.store); else if (2 == a) {
                var n = this.data.fjpx.sort(function(t, a) {
                    t = Number(t.views);
                    return (a = Number(a.views)) < t ? -1 : t < a ? 1 : 0;
                });
                this.setData({
                    store2: n
                });
            }
        }
        this.setData({
            sliderOffset: t.currentTarget.offsetLeft,
            activeIndex: t.currentTarget.id
        });
    },
    redinfo: function(t) {
        var a = wx.getStorageSync("users").id;
        app.util.request({
            url: "entry/wxapp/MyDistribution",
            cachetime: "0",
            data: {
                user_id: a
            },
            success: function(t) {
                console.log(t.data), "2" == t.data.state ? (console.log("是分销商"), wx.navigateTo({
                    url: "../distribution/yaoqing"
                })) : "1" == t.data.state ? wx.showModal({
                    title: "提示",
                    content: "您的申请正在审核中，请耐心等待"
                }) : wx.navigateTo({
                    url: "../distribution/jrhhr"
                });
            }
        });
    },
    jumps: function(t) {
        var a = this, e = (t.currentTarget.dataset.name, t.currentTarget.dataset.appid), n = t.currentTarget.dataset.src, i = (t.currentTarget.dataset.wb_src, 
        t.currentTarget.dataset.id), s = t.currentTarget.dataset.sjtype, r = t.currentTarget.dataset.type;
        if (1 == r) {
            if (console.log(n), "../distribution/jrhhr" == n) return a.redinfo(), !1;
            wx.navigateTo({
                url: n,
                success: function(t) {
                    a.setData({
                        averdr: !0
                    });
                },
                fail: function(t) {},
                complete: function(t) {}
            });
        } else 2 == r ? wx.navigateTo({
            url: "../car/car?vr=" + i + "&sjtype=" + s,
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        }) : 3 == r && wx.navigateToMiniProgram({
            appId: e,
            path: "",
            extraData: {
                foo: "bar"
            },
            envVersion: "develop",
            success: function(t) {
                a.setData({
                    averdr: !0
                });
            }
        });
    },
    onLoad: function(t) {
        var a = decodeURIComponent(t.scene);
        if (console.log("scene", a), "undefined" != a) var e = a;
        if (null != t.userid) {
            console.log("转发获取到的userid:", t.userid);
            e = t.userid;
        }

        var n = this;
        app.util.request({
            url: "entry/wxapp/System",
            cachetime: "0",
            success: function(t) {
                console.log(t), wx.setNavigationBarTitle({
                    title: t.data.pt_name
                }), wx.setStorageSync("color", t.data.color), wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: wx.getStorageSync("color")
                }), 1e4 < Number(t.data.total_num) && (t.data.total_num = (Number(t.data.total_num) / 1e4).toFixed(2) + "万"), 
                n.setData({
                    System: t.data
                });
            }
        }), wx.getSystemInfo({
            success: function(t) {
                n.setData({
                    windowHeight: t.windowHeight
                }), console.log(t);
            }
        }), app.util.request({
            url: "entry/wxapp/Url2",
            cachetime: "0",
            success: function(t) {
                wx.setStorageSync("url2", t.data);
            }
        }), app.util.request({
            url: "entry/wxapp/Llz",
            cachetime: "0",
            data: {
                cityname: wx.getStorageSync("city"),
                type: 2
            },
            success: function(t) {
                console.log(t), n.setData({
                    unitid: t.data
                });
            }
        }), app.util.request({
            url: "entry/wxapp/Url",
            cachetime: "0",
            success: function(t) {
                console.log(t), wx.setStorageSync("url", t.data), n.setData({
                    url: t.data
                });
            }
        });

        if (!wx.getStorageSync("users")){
          this.setData({
            user_info_show: true
          })
        }

        var a = this;
        wx.setNavigationBarColor({
            frontColor: "#ffffff",
            backgroundColor: wx.getStorageSync("color"),
            animation: {
                duration: 0,
                timingFunc: "easeIn"
            }
        }), wx.getSystemInfo({
            success: function(t) {
                a.setData({
                    windowHeight: t.windowHeight
                });
            }
        }), a.setData({
            //store_name: wx.getStorageSync("System").link_name,
            msgList1: wx.getStorageSync("msgList1"),
            System: wx.getStorageSync("System"),
            userinfo: wx.getStorageSync("users")
        }), a.reload(), a.refresh();
    },
    reload: function(t) {
        var s = this, a = wx.getStorageSync("url");
        s.setData({
            url: a
        }), app.util.request({
            url: "entry/wxapp/StoreType",
            cachetime: "0",
            success: function(t) {
                var a = t.data;
                a.length <= 5 ? s.setData({
                    height: 165
                }) : 5 < a.length && s.setData({
                    height: 340
                });
                for (var e = [], n = 0, i = a.length; n < i; n += 10) e.push(a.slice(n, n + 10));
                s.setData({
                    nav: e
                });
            }
        });
        var e = wx.getStorageSync("city");
        app.util.request({
            url: "entry/wxapp/Ad",
            cachetime: "0",
            data: {
                cityname: e
            },
            success: function(t) {
                var a = [];
                for (var e in t.data) 2 == t.data[e].type && a.push(t.data[e]);
                s.setData({
                    slide: a
                });
            }
        });
    },
    indexReload: function (t) {
      var c = this, i = this.data.fxzuid;
      wx.login({
        success: function (t) {
          var a = t.code;
          wx.setStorageSync("code", a), wx.getSetting({
            success: function (t) {
              console.log(t), t.authSetting["scope.userInfo"] ? wx.getUserInfo({
                success: function (t) {
                  console.log(t), wx.setStorageSync("user_info", t.userInfo);
                  var e = t.userInfo.nickName, n = t.userInfo.avatarUrl;
                  app.util.request({
                    url: "entry/wxapp/openid",
                    cachetime: "0",
                    data: {
                      code: a
                    },
                    success: function (t) {
                      console.log(t), wx.setStorageSync("key", t.data.session_key), wx.setStorageSync("openid", t.data.openid);
                      var a = t.data.openid;
                      app.util.request({
                        url: "entry/wxapp/Login",
                        cachetime: "0",
                        data: {
                          openid: a,
                          img: n,
                          name: e
                        },
                        success: function (t) {
                          console.log(t), c.setData({
                            userinfo: t.data
                          }), wx.setStorageSync("users", t.data), wx.setStorageSync("uniacid", t.data.uniacid),
                            null != i && app.util.request({
                              url: "entry/wxapp/Binding",
                              cachetime: "0",
                              data: {
                                fx_user: t.data.id,
                                user_id: i
                              },
                              success: function (t) {
                                console.log(t);
                              }
                            });
                          c.setData({
                            user_info_show: false
                          })
                        }
                      });
                    }
                  });
                }
              }) : (console.log("未授权过"), c.setData({
                hydl: !0
              }));
            }
          });
        },
        fail:function(e){
          console.log(e)
        }
      }), wx.getLocation({
        type: "wgs84",
        success: function (t) {
          wx.setStorageSync("Location", t);
          var a = t.latitude + "," + t.longitude;
          app.util.request({
            url: "entry/wxapp/map",
            cachetime: "0",
            data: {
              op: a
            },
            success: function (i) {
              console.log(i), app.util.request({
                url: "entry/wxapp/System",
                cachetime: "0",
                success: function (t) {
                  console.log(t), "1" == t.data.dw_more && c.setData({
                    dwcity: i.data.result ? i.data.result.address_component.district : ''
                  }), "2" == t.data.dw_more && c.setData({
                    dwcity: i.data.result ? i.data.result.address_component.city : ''
                  });
                  var a = t.data.gd_key;
                  "" == a && wx.showModal({
                    title: "配置提示",
                    content: "请在后台配置高德地图的key",
                    showCancel: !0,
                    cancelText: "取消",
                    confirmText: "确定",
                    success: function (t) { },
                    fail: function (t) { },
                    complete: function (t) { }
                  }), new (require("../amap-wx.js").AMapWX)({
                    key: a
                  }).getWeather({
                    success: function (t) {
                      var a, e, n = t.liveData.city, i = t.liveData.weather, s = t.liveData.reporttime.slice(0, 10), o = (0 == (a = new Date(s)).getDay() && (e = "星期日"),
                        1 == a.getDay() && (e = "星期一"), 2 == a.getDay() && (e = "星期二"), 3 == a.getDay() && (e = "星期三"),
                        4 == a.getDay() && (e = "星期四"), 5 == a.getDay() && (e = "星期五"), 6 == a.getDay() && (e = "星期六"),
                        e), r = t.temperature.data;
                      c.setData({
                        area: n,
                        reporttime: s,
                        weather: i,
                        w1: o,
                        temperature: r
                      });
                    },
                    fail: function (t) { }
                  });
                  var e = ["最新信息"];
                  "1" == t.data.is_sjrz && e.push("热门商家"), "1" == t.data.is_pageopen && "1" == t.data.is_hyqx && e.push("黄页114"),
                    "1" == t.data.is_pcfw && "1" == t.data.is_pcqx && e.push("顺风车"), "1" == t.data.is_hd && "1" == t.data.is_hdqx && e.push("活动报名"),
                    console.log(e), wx.setStorageSync("System", t.data), 1 == t.data.many_city ? (wx.setStorageSync("city", t.data.cityname),
                      c.setData({
                        city: t.data.cityname
                      })) : (console.log(wx.getStorageSync("city_type")), 1 != wx.getStorageSync("city_type") ? (wx.setStorageSync("city", c.data.dwcity),
                        c.setData({
                          city: c.data.dwcity
                        })) : (c.setData({
                          city: wx.getStorageSync("city")
                        }), console.log("choosecity")));
                  var n = wx.getStorageSync("city");
                  console.log(n), app.util.request({
                    url: "entry/wxapp/SaveHotCity",
                    cachetime: "0",
                    data: {
                      cityname: n,
                      user_id: wx.getStorageSync("users").id
                    },
                    success: function (t) {
                      console.log(t);
                    }
                  }), c.setData({
                    bkarr: e
                  });
                }
              });
            }
          });
        },
        fail: function (t) {
          app.util.request({
            url: "entry/wxapp/System",
            cachetime: "0",
            success: function (t) {
              console.log(t);
              var a = t.data.gd_key;
              "" == a && wx.showModal({
                title: "配置提示",
                content: "请在后台配置高德地图的key",
                showCancel: !0,
                cancelText: "取消",
                confirmText: "确定",
                success: function (t) { },
                fail: function (t) { },
                complete: function (t) { }
              }), new (require("../amap-wx.js").AMapWX)({
                key: a
              }).getWeather({
                success: function (t) {
                  var a, e, n = t.liveData.city, i = t.liveData.weather, s = t.liveData.reporttime.slice(0, 10), o = (0 == (a = new Date(s)).getDay() && (e = "星期日"),
                    1 == a.getDay() && (e = "星期一"), 2 == a.getDay() && (e = "星期二"), 3 == a.getDay() && (e = "星期三"),
                    4 == a.getDay() && (e = "星期四"), 5 == a.getDay() && (e = "星期五"), 6 == a.getDay() && (e = "星期六"),
                    e), r = t.temperature.data;
                  c.setData({
                    area: n,
                    reporttime: s,
                    weather: i,
                    w1: o,
                    temperature: r
                  });
                },
                fail: function (t) { }
              });
              var e = ["最新信息"];
              "1" == t.data.is_sjrz && e.push("热门商家"), "1" == t.data.is_pageopen && "1" == t.data.is_hyqx && e.push("黄页114"),
                "1" == t.data.is_pcfw && "1" == t.data.is_pcqx && e.push("顺风车"), "1" == t.data.is_hd && "1" == t.data.is_hdqx && e.push("活动报名"),
                console.log(e), wx.setStorageSync("System", t.data), wx.setStorageSync("city", t.data.cityname),
                c.setData({
                  city: t.data.cityname
                });
              var n = wx.getStorageSync("city");
              console.log(n), c.setData({
                bkarr: e
              }), c.refresh(), c.seller();
            }
          });
        }
      }), app.util.request({
        url: "entry/wxapp/Views",
        cachetime: "0",
        success: function (t) {
          console.log(t);
          var a = t.data;
          "" == a ? a = 0 : 1e4 < Number(a) && (a = (Number(a) / 1e4).toFixed(2) + "万"), c.setData({
            views: a
          });
        }
      }), app.util.request({
        url: "entry/wxapp/Num",
        cachetime: "0",
        success: function (t) {
          c.setData({
            Num: t.data
          });
        }
      });
    },
    refresh: function() {
        var m = this, t = (m.data.star1, wx.getStorageSync("city"));
        console.log("城市为" + t), console.log("page数量为" + m.data.page);
        var x = m.data.page, v = m.data.business;
        null == x && (x = 1), null == v && (v = []), app.util.request({
            url: "entry/wxapp/StoreList",
            cachetime: "0",
            data: {
                page: x,
                cityname: t
            },
            success: function(t) {
                if (console.log(t), 0 == t.data.length) m.setData({
                    refresh_top: !0
                }), 1 == x && m.setData({
                    store: [],
                    business: [],
                    fjpx: [],
                    store1: []
                }); else {
                    m.setData({
                        page: x + 1,
                        refresh_top: !1
                    });
                    for (var a = {}, e = [], n = 0, i = (v = v.concat(t.data)).length; n < i; n++) a[v[n]] || (e.push(v[n]), 
                    a[v[n]] = !0);
                    for (var s in t.data) {
                        t.data[s].star = m.data.star1;
                        t.data[s].star;
                        t.data[s].score = Number(t.data[s].score);
                        t.data[s].score;
                        var r = t.data[s].coordinates.split(",");
                        t.data[s].lat2 = Number(wx.getStorageSync("Location").latitude), t.data[s].lng2 = Number(wx.getStorageSync("Location").longitude);
                        var o = Number(wx.getStorageSync("Location").latitude), c = Number(wx.getStorageSync("Location").longitude), g = r[0], u = r[1], l = o * Math.PI / 180, d = g * Math.PI / 180, f = l - d, p = c * Math.PI / 180 - u * Math.PI / 180, h = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(f / 2), 2) + Math.cos(l) * Math.cos(d) * Math.pow(Math.sin(p / 2), 2)));
                        h *= 6378.137;
                        h = (h = Math.round(1e4 * h) / 1e4).toFixed(2);
                        h = h == 'NaN' ? null : h;
                        t.data[s].distance = h;
                    }
                    m.setData({
                        store: v,
                        business: v,
                        fjpx: v
                    }), m.setData({
                        store1: m.data.fjpx.sort(function(t, a) {
                            return (t = Number(t.distance)) < (a = Number(a.distance)) ? -1 : a < t ? 1 : 0;
                        })
                    });
                }
            }
        }), app.util.request({
            url: "entry/wxapp/news",
            cachetime: "0",
            data: {
                cityname: t
            },
            success: function(t) {
                var a = [];
                for (var e in t.data) 2 == t.data[e].type && a.push(t.data[e]);
                m.setData({
                    msgList: a
                });
            }
        });
    },
    sellted: function(t) {
        wx.navigateTo({
            url: "../settled/settled",
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    store: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../sellerinfo/sellerinfo?id=" + a,
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    notice: function(t) {
        var a = t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../notice/notice?id=" + a,
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    phone: function(t) {
        var a = t.currentTarget.dataset.tel;
        wx.makePhoneCall({
            phoneNumber: a
        });
    },
    store_type_id: function(t) {
        var a = t.currentTarget.dataset.id, e = t.currentTarget.dataset.name;
        wx.navigateTo({
            url: "business?id=" + a + "&typename=" + e,
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    bindinput: function(t) {
        var a = t.detail.value;
        this.setData({
            value: a
        });
    },
    sqss: function() {
        this.setData({
            djss: !1
        });
    },
    search: function(t) {
        var a = this.data.value, e = this;
        console.log(a), e.setData({
            ssjgarr: [],
            djss: !1
        }), "" != a ? app.util.request({
            url: "entry/wxapp/StoreList",
            cachetime: "0",
            data: {
                keywords: a
            },
            success: function(t) {
                console.log(t), e.setData({
                    djss: !0,
                    ssjgarr: t.data
                });
            }
        }) : wx.showToast({
            title: "请输入内容",
            icon: "loading"
        });
    },
    onReady: function() {
        this.setData({
            first: 1
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        this.reload(), this.setData({
            page: 1,
            business: [],
            store: []
        }), this.refresh(), wx.stopPullDownRefresh();
    },
    onReachBottom: function() {
        0 == this.data.refresh_top && this.refresh();
    },
    onShareAppMessage: function() {}
});